package com.example.cash.craft.service.impl;

import com.example.cash.craft.dao.cache.ICategoryCacheRepository;
import com.example.cash.craft.dao.persist.mapper.ProductMapper;
import com.example.cash.craft.dao.persist.repository.ICategoryRepository;
import com.example.cash.craft.exception.ServiceException;
import com.example.cash.craft.pojo.entity.Category;
import com.example.cash.craft.pojo.param.CategoryAddNewParam;
import com.example.cash.craft.pojo.param.CategoryUpdateParam;
import com.example.cash.craft.pojo.vo.CategoryVO;
import com.example.cash.craft.pojo.vo.PageData;
import com.example.cash.craft.service.ICategoryService;
import com.example.cash.craft.web.StateCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements ICategoryService {

    @Value("${cash-craft.config.default-page-size}")
    private Integer defaultPageSize;
    @Autowired
    private ICategoryRepository categoryRepository;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ICategoryCacheRepository categoryCacheRepository;

    @Override
    public void addNew(CategoryAddNewParam param) {
        String name = param.getName();
        int countByName = categoryRepository.countByName(name);
        if (countByName > 0) {
            throw new ServiceException(StateCode.ERROR_CONFLICT, "类别名称已存在");
        }

        Category category = new Category();
        BeanUtils.copyProperties(param, category);
        categoryRepository.insert(category);
    }

    @Override
    public void deleteById(Long id) {
        CategoryVO findResult = categoryRepository.selectById(id);
        if (findResult == null) {
            throw new ServiceException(StateCode.ERROR_NOT_FOUND, "类别不存在");
        }

        int countByCategoryId = productMapper.countByCategoryId(id);
        if (countByCategoryId > 0) {
            throw new ServiceException(StateCode.ERROR_FORBIDDEN, "类别下有商品，不能删除");
        }

        categoryRepository.deleteById(id);
    }

    @Override
    public void updateById(Long id, CategoryUpdateParam param) {
        CategoryVO findResult = categoryRepository.selectById(id);
        if (findResult == null) {
            throw new ServiceException(StateCode.ERROR_NOT_FOUND, "类别不存在");
        }

        Category category = new Category();
        BeanUtils.copyProperties(param, category);
        category.setId(id);
        categoryRepository.updateById(category);
    }

    @Override
    public CategoryVO selectById(Long id) {
        CategoryVO findResult = categoryCacheRepository.selectById(id);
        if (findResult == null) {
            throw new ServiceException(StateCode.ERROR_NOT_FOUND, "类别不存在");
        }
        return findResult;
    }

    @Override
    public PageData<CategoryVO> listAll(Integer page) {
        return categoryCacheRepository.listAll(page, defaultPageSize);
    }

    @Override
    public void rebuildCache() {
        categoryCacheRepository.deleteList();
        categoryCacheRepository.deleteAllItems();
        categoryCacheRepository.deleteKeysSet();

        Integer page = 1;
        Integer pageSize = defaultPageSize;
        Integer maxPage;
        do {
            PageData<CategoryVO> pageData = categoryRepository.listAll(page, pageSize);
            maxPage = pageData.getMaxPage();
            List<CategoryVO> list = pageData.getList();
            categoryCacheRepository.saveAll(list);
            for (CategoryVO item : list) {
                categoryCacheRepository.save(item);
            }
            page++;
        } while (page <= maxPage);
    }

}
